package
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.utils.Timer;

	[SWF(backgroundColor=0x000000,width="760",height=600,frameRate="24",alpha="0.2")]
	public class ZombieLoading extends Sprite
	{
		private var loadingTipArr:Array=["正在加载主程序. . .",  "正在加载资源. . .", "加载模型动画. . .","正在加载僵尸模型皮肤...","请求玩家数据...","初始化家园跟好友数据...","正在加载开场动画。。。"];
		private var loadBg:MovieClip;
		
		/**
		 * 服务器的跟目录 
		 */		
		private var rootUrl:String;
		private var _loaderConfig:URLLoader;
		private var _initZombieTown:String;
//		private var _mainUiUrl:String;
		private var _navigateVideoUrl:String;
		private var loader:Loader;
		
		/** 加载条倍数 **/
		private var loadProcessMultipleNum:Number;
		/** 加载条基数 **/
		private var loadProcessBaseNum:uint;
		
		private var xml:XML;
		
		/**
		 * 用来清空缓存的参数 
		 */		
		public var pageParams:String="?0";
		/**
		 * 平台信息 
		 */		
		private var param:Object;
		
		private var mv:MovieClip;
		public function ZombieLoading()
		{
			Security.allowDomain("*");
			
			param = this.stage.loaderInfo.parameters;//平台参数
			this.pageParams="?"+param.r;
			
			this._loaderConfig=new URLLoader();
			this._loaderConfig.addEventListener(Event.COMPLETE, loadFlatConfigComplete);
			
			rootUrl=this.stage.loaderInfo.parameters["url"];
			
			if (!rootUrl)
			{
				rootUrl="";
			}
			this._loaderConfig.load(new URLRequest(rootUrl + "config.xml"+this.pageParams));
			
			ExternalInterface.addCallback("sendFeedOK",sendFeedOK);
			ExternalInterface.addCallback("buyShopOk",buyShopOk);
			
		}
		
		private function sendFeedOK():void
		{
			this.main.sendFeedOK();
		}
		private function buyShopOk(itemId:String):void
		{
			this.main.buyShopOk(itemId);
		}
		
		private function loadFlatConfigComplete(e:Event):void
		{
			this._loaderConfig.removeEventListener(Event.COMPLETE,loadFlatConfigComplete);
			
			var loadData:Object=(e.target as URLLoader).data;
			
			xml=XML(loadData);
			var cdn:String=xml.cdn;
			
			this._initZombieTown=cdn+xml.initZombieTownUrl+this.pageParams;
			this._navigateVideoUrl=cdn+xml.openCGUrl+this.pageParams;
			
			
			initView();
			
			//开始下载主程序
			var loadContext:LoaderContext=new LoaderContext();
			loadContext.applicationDomain=ApplicationDomain.currentDomain;
			this.loader=new Loader();
			this.loadProcessBaseNum=0;
			this.loadProcessMultipleNum=0.1;
			this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProcessBarHandler);
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, success);
			this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			this.loader.load(new URLRequest(this._initZombieTown), loadContext);
			loadSourceTxt=this.loadingTipArr[0];
			this.stage.scaleMode=StageScaleMode.NO_SCALE;
			
		}
		
		/**
		 * 初始化视图 
		 */		
		private function initView():void
		{
			loadBg=new ZombieLodingUI();
			this.addChild(this.loadBg);
			mv 	= 	loadBg.getChildByName("loadingBar") as MovieClip;
			mv.gotoAndStop(0);
		}
					
		private var main:*;
		private var initMain:Object;
		//主程1成功
		private function success(evt:Event):void
		{
			var info:LoaderInfo=LoaderInfo(evt.target);
			
			var domain:ApplicationDomain=info.applicationDomain;
			var initMainClass:Class=domain.getDefinition("InitZombieTown") as Class;
			initMain=new initMainClass();
			
			this.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProcessBarHandler);
			this.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, success);
			this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
			initMain.callback=loadMainCompleteFunc;
			processData();
		}
		
		
		/**
		 * 初始化数据
		 */
		public function processData():void
		{
			initMain.start();
		}
		
		
		/**
		 * 主程序2
		 * @param evt
		 */
		private function loadMainCompleteFunc(_main:Class):void
		{
			loadSourceTxt=this.loadingTipArr[1];
			var manorPanelcc:Class=_main;
			main=new manorPanelcc();
			var uiLoader:Loader=new Loader();
			this.loadProcessBaseNum=10;
			this.loadProcessMultipleNum=0.1;
			
			main.mainXML=this.xml;
			main.initLogin(param,loadOverCallBackFun,loadStep,this.pageParams,loadProcessBarUIHandler);
			
//			var loadConext:LoaderContext=new LoaderContext();
//			loadConext.applicationDomain=ApplicationDomain.currentDomain.parentDomain;
//			uiLoader.load(new URLRequest(this._mainUiUrl));
//			uiLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadUIComplete);
//			uiLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProcessBarHandler);
			
		}
		
		/**
		 * 加载游戏UI
		 * @param event
		 */
		private function loadUIComplete(event:Event):void
		{
			LoaderInfo(event.target).removeEventListener(Event.COMPLETE, loadUIComplete);
			LoaderInfo(event.target).removeEventListener(ProgressEvent.PROGRESS, loadProcessBarHandler);
			var loadInfo:LoaderInfo=event.target as LoaderInfo;
			
			loadSourceTxt="正在加载资源...";
			
			main.initLogin(param,loadOverCallBackFun,loadStep,this.pageParams,loadInfo);
		}
		
		
		
		
		private function onIoError(evt:IOErrorEvent):void
		{
			throw Error("loader main fail!!");
		}
		
		/**
		 * 统一处理
		 * @param evt
		 */
		private function loadProcessBarHandler(evt:ProgressEvent):void
		{
			this.loadProcessTxt=(int(Number(100 * evt.bytesLoaded / evt.bytesTotal) * loadProcessMultipleNum) + loadProcessBaseNum) + "%";
			this.mv.gotoAndStop((int(Number(100 * evt.bytesLoaded / evt.bytesTotal) * loadProcessMultipleNum) + loadProcessBaseNum));
		}
		
		private function loadProcessBarUIHandler(evt:ProgressEvent):void
		{
			this.loadProcessTxt=(int(Number(400 * evt.bytesLoaded / evt.bytesTotal) * loadProcessMultipleNum) + loadProcessBaseNum) + "%";
			this.mv.gotoAndStop((int(Number(400 * evt.bytesLoaded / evt.bytesTotal) * loadProcessMultipleNum) + loadProcessBaseNum));
		}
		
		/**
		 * 加载完成 
		 * isNew 是否是新手  0老玩家  1是新手
		 */		
		private var isNewPlayer:int;
		private var timer:Timer;
		private function loadOverCallBackFun(isNew:int):void
		{
			stage.addChild(main);
			this.main.enterGame();
			stage.addChild(this);
			
			this.isNewPlayer=isNew;
			this.loadProcessTxt="86%";//
			this.mv.gotoAndStop(86);//
			loadSourceTxt=this.loadingTipArr[5];
			
			timer=new Timer(800,14);//800
			timer.addEventListener(TimerEvent.TIMER,timerHalder);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
			timer.start();
		}
		
		private var index:int=1;
		private function timerHalder(evt:TimerEvent):void
		{
			this.loadProcessTxt=(86+index)+"%";//
			this.mv.gotoAndStop(86+index);//
			index++;
		}
		
		private function completeTimerHalder(evt:TimerEvent):void
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER,timerHalder);
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeTimerHalder);
			timer=null;
			if(isNewPlayer==1)
			{
				//加载开场动画
				this.loadProcessTxt="";
				this.mv.gotoAndStop(1);
				loadSourceTxt=this.loadingTipArr[6];
				var load:Loader=new Loader();
				this.loadProcessMultipleNum=1;
				this.loadProcessBaseNum=0;
				load.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoginVideo);
				load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProcessBarHandler);
				load.load(new URLRequest(this._navigateVideoUrl));
			}else{
				enterGame();
			}
		}
		
		private var _videoObj:*=null;
		private function completeLoginVideo(e:Event):void
		{
			(e.target as LoaderInfo).removeEventListener(Event.COMPLETE, completeLoginVideo);
			(e.target as LoaderInfo).removeEventListener(ProgressEvent.PROGRESS, loadProcessBarHandler);
			this._videoObj=e.currentTarget.content;
			this.addChild(this._videoObj);
			this._videoObj.addEventListener(Event.ENTER_FRAME, videoEnterFrameFunc);
			this._videoObj.play();
			
		}
		
		/**
		 * 播放开场动画
		 * @param e
		 */
		private function videoEnterFrameFunc(e:Event):void
		{
			if (this._videoObj.currentFrame >= this._videoObj.totalFrames)
			{
				this._videoObj.stop();
				this._videoObj.removeEventListener(Event.ENTER_FRAME, videoEnterFrameFunc);
				this.removeChild(this._videoObj);
				this._videoObj=null;
				enterGame();
			}
		}
		
		/**
		 * 进入主游戏 
		 */		
		private function enterGame():void
		{
			if(int(xml.isPlatformProviderFeed)==1)
			{
				main.setFeedFunc(platFeed);
			}
			stage.removeChild(this);
			if(this.isNewPlayer==1)
			{
				this.main.openSelectSexView();
			}else{
				this.main.playGameMusic();
			}
		}
		
		
		/**
		 * 加载进度
		 * @param step
		 */
		private function loadStep(step:int):void
		{
			this.loadProcessTxt=(50 + step * 12) + "%";
			this.mv.gotoAndStop(50 + step * 12);
			loadSourceTxt=this.loadingTipArr[step+1];
		}
		
		
		public function set loadProcessTxt(value:String):void
		{
			(loadBg.getChildByName("loadProcessTxt") as TextField).text=value;
		}
		public function set loadSourceTxt(value:String):void
		{
			(loadBg.getChildByName("loadSourceTxt") as TextField).text=value;
		}
		
		/**
		 * 平台新鲜事
		 * @param useId
		 */
		public function platFeed(feedObj:Object):void
		{
			if (ExternalInterface.available)
			{
				ExternalInterface.call("sendFeed", feedObj);
			}
		}

	}
}